import Vue from 'vue'
import VueRouter from 'vue-router'
import Index from '../views/Index.vue'
import {http} from "../network/index"
import {Toast} from 'vant'

Vue.use(VueRouter)

const routes = [
    {
        path: "/",
        name: 'Index',
        component: Index,
        children: [
            {
                path: '/',
                name: 'Home',
                component: () => import('../views/Home.vue')
            },
            {
                path: '/about',
                name: 'About',
                component: () => import('../views/About.vue')
            },
            {
                path: '/study',
                name: "Study",
                component: () => import('../views/Study.vue')
            },
            {
                path: '/lx',
                name: "Lx",
                component: () => import('../views/Lx.vue')
            },
            {
                path: '/user',
                name: "User",
                meta: {
                    isAuth: true
                },
                component: () => import('../views/User.vue')
            }
        ]
    },
    //登录页面
    {
        path: '/logon',
        name: "Logon",
        component: () => import('../views/userLogon/Logon.vue')
    },
    //退出登录页面
    {
        path: '/fit',
        name: "Fit",
        component: () => import('../views/user/Fit.vue')
    },
    //设置密码页面
    {
        path: '/fitPassword',
        name: "FitPassword",
        component: () => import('../views/user/FitPassword.vue')
    },
    //课程详情页面
    {
        path: '/course',
        name: "Course",
        component: () => import('../views/details/Course')
    },
    //老师详情页面
    {
        path: '/teacher',
        name: "Teach",
        component: () => import('../views/details/Teacher')
    },
    //user用户详细信息及修改页面
    {
        path: '/personal',
        name: "Personal",
        component: () => import('../views/userLogon/Personal')
    },
    //设置用户信息页面
    {
        path: '/set-info',
        name: "SetInfo",
        component: () => import('../views/user/SetInfo')
    },
    //立即预约
    {
        path: '/order',
        name: "Order",
        component: () => import('../views/details/Order.vue')
    },
    //我的中的收藏页面
    {
        path: '/collect',
        name: "Collect",
        component: () => import('../views/user/Collect')
    },
    //attention
    {
        path: '/attention',
        name: "Attention",
        component: () => import('../views/user/attention')
    },
    //搜索
    {
        path: '/search',
        name: "Search",
        component: () => import('../views/search/search')
    },
    //视频
    {
        path: '/videokc',
        name: "Videokc",
        component: () => import('../views/video/Videokc')
    },
    //约课预约
    {
        path: '/oto',
        name: "Oto",
        component: () => import('../views/Study/Oto')
    },
    //练题列表
    {
        path: '/special',
        name: "Special",
        component: () => import('../views/lx/Special')
    },
    //题库
    {
        path: '/point',
        name: "Point",
        component: () => import('../views/lx/Point')
    },
    //题目,刷题页面
    {
        path: '/topic',
        name: "Topic",
        component: () => import('../views/topic/Topic')
    },
    //题目.....
    {
        path: '/testPaper',
        name: "TestPaper",
        component: () => import('../views/lx/TestPaper')
    },
    //题目.......
    {
        path: '/exam',
        name: "Exam",
        component: () => import('../views/lx/Exam')
    },
    //反馈
    {
        path: '/liuyan',
        name: "Liuyan",
        component: () => import('../views/video/Liuyan')
    },
]
let arr = {}

const router = new VueRouter({
    mode: 'history',
    base: process.env.BASE_URL,
    routes,

    //页面之间无位置影响
    scrollBehavior(to, from, savedPosition) {
        let y = arr[to.name]
        return {x: 0, y: y}
    }
})

router.beforeEach((to, from, next) => {
    arr[from.name] = document.documentElement.scrollTop
    // console.log('to:', to)
    if (to.meta.isAuth) {

        let token = localStorage.getItem("adminToken");

        if (!token) {
            Toast.fail('请先登录')
            next("/logon");
        }

        http.userInfo().then(res => {
            let code = null;
            code = res.data.code
            if (code !== 200) {
                Toast.fail('登录超时,重新登录!')

                localStorage.removeItem('adminToken')
                next("/logon");
            }
        })
    }

    next()
})

export default router
